package com.liuchuanzheng.lcz_compose_news.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel

/**
 * @author 刘传政
 * @date 2021/8/2 10:51
 * QQ:1052374416
 * 电话:18501231486
 * 作用:
 * 注意事项:
 */
class MainViewModel(private val savedStateHandle: SavedStateHandle):ViewModel() {
    private val HOME_PAGE_SELECTED_INDEX = "home_page_selected_index"
    private val mSelectLiveData = MutableLiveData<Int>()
    fun getSelectedIndex(): MutableLiveData<Int> {
        if (mSelectLiveData.value == null){
            val index = savedStateHandle.get<Int>(HOME_PAGE_SELECTED_INDEX)?:0
            mSelectLiveData.postValue(index)
        }
        return mSelectLiveData
    }
    fun saveSelectIndex(selectIndex:Int){
        savedStateHandle.set(HOME_PAGE_SELECTED_INDEX,selectIndex)
        mSelectLiveData.value = selectIndex
    }
}